package club.ljwang.controller;

import club.ljwang.entity.PageResult;
import club.ljwang.entity.QueryPageBean;
import club.ljwang.entity.Result;
import club.ljwang.pojo.Role;
import club.ljwang.pojo.User;
import club.ljwang.service.UserNameService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RequestMapping("userName")
@RestController
public class UserNameController {

    @Reference
    private UserNameService userNameService;

    //分页
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return userNameService.findPage(queryPageBean);
    }

    //查询所有角色
    @RequestMapping("findAll")
    public Result queryall() {
        List<Role> list = userNameService.queryall();
        return new Result(true, "", list);
    }

    //添加
    @RequestMapping("add")
    public Result add(@RequestBody User user) {
        try {
            userNameService.add(user);
            //新增成功
            return new Result(true, "新增用户成功");
        } catch (Exception e) {
            //新增失败
            return new Result(false, "新增用户失败");
        }
    }

    @RequestMapping("findAll4Edit")
    //弹出编辑框查询
    public Result findAll4Edit(@RequestParam("id") Integer id) {
        Map userMap = userNameService.findAll4Edit(id);
        return new Result(true, "", userMap);
    }

    //编辑
    @RequestMapping("enit")
    public Result enit(@RequestBody User user) {
        try {
            userNameService.enit(user);
            return new Result(true, "编辑用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑用户失败");
        }
    }

    //删除
    @RequestMapping("delete")
    public Result delrte(Integer id) {
        try {
            userNameService.delete(id);
        } catch (Exception e) {
            return new Result(false, "用户删除失败");
        }
        return new Result(true, "用户删除成功");
    }
}